package com.lenovo.newsapp.fragment;

import java.util.List;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.activity.MainActivity;
import com.lenovo.newsapp.activity.NewsDetailImgActivity;
import com.lenovo.newsapp.util.Const;

public class StockListFragment extends Fragment {
	private WebView web;
	private MainActivity main;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		main = (MainActivity) activity;

	}

	@SuppressWarnings("deprecation")
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
//		Toast.makeText(main, R.string.not_been_enabled, Toast.LENGTH_LONG).show();
		setView();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.listview_stock, container,
				false);
		web = (WebView) rootView.findViewById(R.id.web);
		return rootView;
	}

	private void setView() {
		Log.v(Const.DEBUG, "flase--" + checkinstallornotadobeflashapk());
		if (checkinstallornotadobeflashapk()) {
			WebSettings settings = web.getSettings();
			settings.setPluginsEnabled(true);
			settings.setJavaScriptEnabled(true);
			settings.setAllowFileAccess(true);
			settings.setDefaultTextEncodingName("GBK");
			web.setBackgroundColor(0);
			// web.loadUrl("file:///android_asset/abc.swf");
			// web.loadUrl("http://www.quamnet.com/quote/0992/LenovoQuote.swf?CID=0992&lang=en");
			// web.loadUrl("http://192.168.1.123:9000/application/stock");
//			web.loadUrl("http://oem.taskcity.com/henghuatype1");
//			 web.loadUrl(Const.DOMAIN+"/application/stock");
			 web.loadUrl("file:///android_asset/stock.html");
		}else{
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Const.FLASH_DOWNLOAD_URL));
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);

		}

	}

	// 检查机子是否安装的有Adobe Flash相关APK
	private boolean checkinstallornotadobeflashapk() {
		final PackageManager packageManager = main.getPackageManager();
		// 获取所有已安装程序的包信息
		List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
		for (int i = 0; i < pinfo.size(); i++) {
			if (pinfo.get(i).packageName
					.equalsIgnoreCase("com.adobe.flashplayer"))
				return true;
		}
		return false;

	}

	private PackageManager getPackageManager() {
		// TODO Auto-generated method stub
		return null;
	}
}
